import socket from '@/utils/socket.js'
import { storeToRefs } from 'pinia'

import { useSimpleTaskData } from '@/stores/simpleTaskData.js'
import { useSimpleUIStore } from '@/stores/simpleTaskUI.js'

const simpleTaskData = useSimpleTaskData()
const { currentIndex, indicator, indicators } = storeToRefs(simpleTaskData)
const { getOptionObj, PFs, allTaskData } = simpleTaskData

const simpleUIStore = useSimpleUIStore()
const { UIOnSimpleSubmit } = simpleUIStore
export default function selectChange() {
  const carousel = ref()
  const calcIndicator = (value) => {
    if (allTaskData.length == 0)
      return
    if (indicator.value in allTaskData[currentIndex.value]) {
      carousel.value.setActiveItem(indicator.value)
      return
    }
    allTaskData[currentIndex.value][indicator.value] = getOptionObj()
    // 更新UI
    UIOnSimpleSubmit()
    nextTick(() => {
      carousel.value.setActiveItem(indicator.value)
    })
    socket.send({
      messageType: '9',
      indicator_data: indicators.value[indicator.value],
      'current_index': currentIndex.value
    })

  }
  return {
    calcIndicator,
    carousel
  }
}
